package cn.hn.monkey.stock.tools;

import android.util.Log;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ZipTools {

    final static int BUFFER = 1024;

    public interface ZipToolsListener {
        void onUnzipFile(final String filename, final byte[] buffer, final int size);
        void onUnzipStatus(boolean begin, boolean end);
    }

    public static String unzip(String zipFilePath, String unzipDir) {
        return unzip(zipFilePath, unzipDir, null);
    }

    public static String unzip(String zipFilePath, final ZipToolsListener listener) {
        return unzip(zipFilePath, null, listener);
    }

    public static String unzip(String zipFilePath, String unzipDir, final ZipToolsListener listener) {
        String path = "";
        BufferedInputStream bis;
        BufferedOutputStream bos;
        try {
            ZipEntry entry;
            ZipFile zipFile = new ZipFile(zipFilePath);

            Enumeration dir = zipFile.entries();
            while(dir.hasMoreElements()) {
                entry = (ZipEntry) dir.nextElement();
                if(entry.isDirectory() && unzipDir != null) {
                    path = entry.getName();
                    path = path.substring(0, path.length() - 1);
                    File fileObj = new File(unzipDir + File.separator + path);
                    fileObj.mkdir();
                }
            }
            Enumeration fil = zipFile.entries();
            while(fil.hasMoreElements()) {
                entry = (ZipEntry) fil.nextElement();
                if (entry.isDirectory()) {
                    continue;
                } else {
                    int count;
                    byte[] dataByte = new byte[BUFFER];
                    bis = new BufferedInputStream(zipFile.getInputStream(entry));
                    String filename = entry.getName();
                    if(listener != null) {
                        while ((count = bis.read(dataByte, 0, BUFFER)) != -1) {
                            listener.onUnzipFile(filename, dataByte, count);
                        }
                    } else {
                        String fullname = unzipDir + File.separator + filename;
                        Log.e("ZipUtil", "fullname:" + fullname);
                        FileOutputStream fos = new FileOutputStream(fullname);
                        bos = new BufferedOutputStream(fos, BUFFER);
                        while ((count = bis.read(dataByte, 0, BUFFER)) != -1) {
                            bos.write(dataByte, 0, count);
                        }
                        bos.flush();
                        bos.close();
                    }
                    bis.close();
                }
            }

        } catch(Exception e) {
            e.printStackTrace();
        }
        return path;
    }
}
